package com.sdp.th.db.th_eq_state_log.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdp.th.db.th_eq_info.service.IThEqInfoService;
import com.sdp.th.db.th_eq_state_log.mapper.ThEqStateLogMapper;
import com.sdp.th.db.th_eq_state_log.service.IThEqStateLogService;
import com.sdp.th.entity.ThEqInfo;
import com.sdp.th.entity.ThEqStateLog;

/**
 * <p>
 * 设备实时信息 服务实现类
 * </p>
 *
 * @author zsp
 * @since 2025-04-12
 */
@Service
public class ThEqStateLogServiceImpl extends ServiceImpl<ThEqStateLogMapper, ThEqStateLog> implements IThEqStateLogService {

	@Autowired
	private IThEqInfoService iThEqInfoService;
	
	@Override
	public void insertEqStateLog(ThEqStateLog stateLog) {
		ThEqInfo eqInfo = iThEqInfoService.queryEqInfoByNo(stateLog.getDeviceNo());
		if(eqInfo!=null) {
			stateLog.setDeviceRoom(eqInfo.getDeviceRoom());
			stateLog.setDeviceRegion(eqInfo.getDeviceRegion());
			stateLog.setDeviceName(eqInfo.getDeviceName());
			save(stateLog);
		}
	}

	@Override
	public List<ThEqStateLog> queryStateLogByRoomAndDate(String deviceRoom, LocalDateTime st, LocalDateTime et) {
		LambdaQueryWrapper<ThEqStateLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(ThEqStateLog::getDeviceRoom, deviceRoom);
		lambdaQueryWrapper.ge(ThEqStateLog::getCreateTime, st);
		lambdaQueryWrapper.le(ThEqStateLog::getCreateTime, et);
		lambdaQueryWrapper.orderByAsc(ThEqStateLog::getCreateTime);
		return list(lambdaQueryWrapper);
	}

	@Override
	public List<ThEqStateLog> queryStateLogByEqAndDate(String deviceNo, LocalDateTime st, LocalDateTime et) {
		LambdaQueryWrapper<ThEqStateLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(ThEqStateLog::getDeviceNo, deviceNo);
		lambdaQueryWrapper.ge(ThEqStateLog::getCreateTime, st);
		lambdaQueryWrapper.le(ThEqStateLog::getCreateTime, et);
		lambdaQueryWrapper.orderByAsc(ThEqStateLog::getCreateTime);
		return list(lambdaQueryWrapper);
	}


}
